package org.soulraven.games.memory.ui.config;

import java.awt.Component;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soulraven.games.memory.model.GameMode;
import org.soulraven.games.memory.model.Player;
import org.soulraven.games.memory.util.MessageUtil;

public class SinglePlayerPanel extends JPanel implements ConfigPanel {

	private static final long serialVersionUID = 6896387422725223803L;

	private JTextField playerNameTextField = null;

	public SinglePlayerPanel() {
		this.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		this.add(new JLabel(MessageUtil.getMessage("singleplayername")));
		this.add(getPlayerNameTextField());
	}

	/**
	 * @return the playerName
	 */
	private JTextField getPlayerNameTextField() {
		if (playerNameTextField == null) {
			playerNameTextField = new JTextField();
			playerNameTextField.setSize(120, 20);
			playerNameTextField.setPreferredSize(playerNameTextField.getSize());
		}
		return playerNameTextField;
	}

	public String getPlayerName() {
		return getPlayerNameTextField().getText().trim();
	}

	@Override
	public GameMode getGameMode() {
		return GameMode.SINGLE_PLAYER_MODE;
	}

	@Override
	public boolean isDataEntered() {
		return (!"".equals(getPlayerName()));
	}

	@Override
	public void showWarning(final Component parent) {
		String message = MessageUtil.getMessage("emptyplayername");
		String title = MessageUtil.getMessage("warning");
		JOptionPane.showMessageDialog(parent, message, title,
				JOptionPane.WARNING_MESSAGE);
	}

	@Override
	public List<Player> getPlayers() {
		List<Player> players = new ArrayList<Player>();
		players.add(new Player(1, getPlayerName()));
		return players;
	}
}
